package com.kuaiditu.assistant.module.retrofit.impl;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantRequest;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantResponse;
import com.kuaiditu.assistant.module.retrofit.ConverterAdapter;
import com.kuaiditu.assistant.module.storage.AssistantUserStorage;
import com.kuaiditu.assistant.ui.authority.AuthorityActivity;
import com.kuaiditu.assistant.ui.login.LoginActivity;

import java.io.IOException;

/**
 * Created by Eenie on 2017/8/31.
 * Email: eeniework@gmail.com
 * Des:
 */

public class AssistantConverterAdapter implements ConverterAdapter<AssistantRequest, AssistantResponse> {

    private AssistantUserStorage mAssistantUserStorage;
    private AssistantApi mAssistantApi;

    public AssistantConverterAdapter(AssistantUserStorage assistantUserStorage, AssistantApi assistantApi) {
        mAssistantApi = assistantApi;
        mAssistantUserStorage = assistantUserStorage;
    }

    @Override
    public String onConverterRequest(AssistantRequest request) throws IOException {
        if (request.isNeedToken() && request.isForceLogin()) {
            if (mAssistantUserStorage.isLogin()) {
                int check = mAssistantUserStorage.getUser().getCourier_checkFlag();
                int auth = mAssistantUserStorage.getUser().getCourier_authFlag();
                request.addDataExtraField(request.getTokenKeyName(), mAssistantUserStorage.getToken());
                if (check == 1) {
                } else {
                    if (auth == 1) {
//                        ARouter.getInstance().build(AuthorityActivity.PATH).greenChannel().navigation();
                        throw new IOException("认证已提交，请耐心等待审核");
                    } else {
                        ARouter.getInstance().build(AuthorityActivity.PATH).greenChannel().navigation();
                        throw new IOException("请提交审核");
                    }
                }
            } else {
                ARouter.getInstance().build(LoginActivity.PATH).greenChannel().navigation();
                throw new IOException("请先登录账号");
            }
        } else if (request.isNeedToken() && !request.isForceLogin()) {
            request.addDataExtraField(request.getTokenKeyName(), mAssistantUserStorage.getToken());
        }
        return request.build();
    }

    @Override
    public String onConverterResponse(AssistantResponse response) throws IOException {
        if (response.isSuccess()) {
            if (TextUtils.isEmpty(response.getRespData())) {
                return "";
            } else {
                return JSON.toJSONString(response);
            }
        } else {
            throw new IOException(response.getRespMsg());
        }
    }

    @Override
    public AssistantResponse strToObj(String str) {
        return JSON.parseObject(str, AssistantResponse.class);
    }


}
